__author__ = 'justinarmstrong'

import pygame as pg
from .. import setup, tools
from .. import constants as c
from .. components import info, mario


class Menu(tools._State):
    def __init__(self):
        """Initializes the state"""
        tools._State.__init__(self)
        persist = {c.COIN_TOTAL: 0,     ###硬币总数
                   c.SCORE: 0,          ###分数
                   c.LIVES: 3,          ###生命值
                   c.TOP_SCORE: 0,      ###最高分数
                   c.CURRENT_TIME: 0.0, ###当前时间
                   c.LEVEL_STATE: None, ###关卡状态
                   c.CAMERA_START_X: 0, ###摄像机开始位置
                   c.MARIO_DEAD: False} ###人物的状态
        self.startup(0.0, persist)

    def startup(self, current_time, persist):
        """Called every time the game's state becomes this one.  Initializes
        certain values"""
        self.next = c.LOAD_SCREEN ###设置下一个状态为加载屏幕。
        self.persist = persist    ###将传入的持久化信息字典保存到self.persist。
        self.game_info = persist  ###将持久化信息字典也保存到self.game_info，用于在游戏信息显示等功能中使用。
        self.overhead_info = info.OverheadInfo(self.game_info, c.MAIN_MENU)
                                  ###使用游戏信息和当前状态（主菜单），初始化一个用于显示游戏顶部信息的对象。
        self.sprite_sheet = setup.GFX['title_screen']
                                  ###从资源字典中获取标题屏幕的图像，用于菜单背景。
        self.setup_background()   ###调用方法设置菜单背景。
        self.setup_mario()        ###调用方法放置Mario角色在屏幕上。
        self.setup_cursor()       ###调用方法创建并初始化菜单选择光标。


    def setup_cursor(self):

        self.cursor = pg.sprite.Sprite()
        dest = (220, 358)
        self.cursor.image, self.cursor.rect = self.get_image(
            24, 160, 8, 8, dest, setup.GFX['item_objects'])
        self.cursor.state = c.PLAYER1  ###表示默认选择为1玩家游戏模式


    def setup_mario(self):

        self.mario = mario.Mario()
        self.mario.rect.x = 110
        self.mario.rect.bottom = c.GROUND_HEIGHT


    def setup_background(self):
        ###从资源字典中加载第一关的背景图像
        self.background = setup.GFX['level_1']
        ###获取背景图像的矩形区域
        self.background_rect = self.background.get_rect()
        ###缩放背景图像到指定倍数（由c.BACKGROUND_MULTIPLER定义）
        self.background = pg.transform.scale(self.background,
                                   (int(self.background_rect.width*c.BACKGROUND_MULTIPLER),
                                    int(self.background_rect.height*c.BACKGROUND_MULTIPLER)))
        ###设置视口矩形，基于屏幕的底部
        self.viewport = setup.SCREEN.get_rect(bottom=setup.SCREEN_RECT.bottom)
        ###初始化image_dict字典，用于存储各种图像及其矩形
        self.image_dict = {}
        ###调用get_image方法，获取游戏名称框的图像和矩形，设置其位置为(170, 100)，并将结果保存到image_dict中
        self.image_dict['GAME_NAME_BOX'] = self.get_image(
            1, 60, 176, 88, (170, 100), setup.GFX['title_screen'])



    def get_image(self, x, y, width, height, dest, sprite_sheet):

        image = pg.Surface([width, height])
        rect = image.get_rect()

        image.blit(sprite_sheet, (0, 0), (x, y, width, height))
        if sprite_sheet == setup.GFX['title_screen']:
            image.set_colorkey((255, 0, 220))
            image = pg.transform.scale(image,
                                   (int(rect.width*c.SIZE_MULTIPLIER),
                                    int(rect.height*c.SIZE_MULTIPLIER)))
        else:
            image.set_colorkey(c.BLACK)
            image = pg.transform.scale(image,
                                   (int(rect.width*3),
                                    int(rect.height*3)))

        rect = image.get_rect()
        rect.x = dest[0]
        rect.y = dest[1]
        return (image, rect)


    def update(self, surface, keys, current_time):
        ###更新当前时间为current_time。
        self.current_time = current_time
        ###更新游戏信息字典中的当前时间
        self.game_info[c.CURRENT_TIME] = self.current_time
        ###调用update_cursor方法，根据按键更新光标位置
        self.update_cursor(keys)
        ###更新顶部信息显示
        self.overhead_info.update(self.game_info)
        ###将背景、游戏名称框、Mario图像和光标图像绘制到surface上
        surface.blit(self.background, self.viewport, self.viewport)
        surface.blit(self.image_dict['GAME_NAME_BOX'][0],
                     self.image_dict['GAME_NAME_BOX'][1])
        surface.blit(self.mario.image, self.mario.rect)
        surface.blit(self.cursor.image, self.cursor.rect)
        ###调用overhead_info.draw方法在屏幕上绘制顶部信息
        self.overhead_info.draw(surface)


    def update_cursor(self, keys):
        ###定义input_list包含三个键：回车、A键和S键，用于在菜单中进行选择。
        input_list = [pg.K_RETURN, pg.K_a, pg.K_s]
        ###如果光标状态为c.PLAYER1，设置光标的Y坐标为358。
        if self.cursor.state == c.PLAYER1:
            self.cursor.rect.y = 358
            if keys[pg.K_DOWN]:
                self.cursor.state = c.PLAYER2
            for input in input_list:
                if keys[input]:
                    self.reset_game_info()
                    self.done = True
        elif self.cursor.state == c.PLAYER2:
            self.cursor.rect.y = 403
            if keys[pg.K_UP]:
                self.cursor.state = c.PLAYER1


    def reset_game_info(self):

        self.game_info[c.COIN_TOTAL] = 0
        self.game_info[c.SCORE] = 0
        self.game_info[c.LIVES] = 3
        self.game_info[c.CURRENT_TIME] = 0.0
        self.game_info[c.LEVEL_STATE] = None

        self.persist = self.game_info
















